%tensorflow_version 1.14 #--------------------------------Setting version of tension flow in colab
import numpy as np
from tensorflow.python.keras.models import Sequential, Model
from tensorflow.keras.optimizers import Adam,SGD
import tensorflow.python.keras
from tensorflow.python.keras.layers.advanced_activations import LeakyReLU
from tensorflow.python.keras.layers import ZeroPadding2D ,BatchNormalization, Activation
from tensorflow.python.keras.layers.convolutional import UpSampling2D, Conv2D
from tensorflow.python.keras.layers import Reshape,Input, Dense, Dropout, Flatten
import matplotlib.pyplot as plt
from tqdm import tqdm
image_shape = (32, 32, 3) #-------------------- Setting the size of image as we have image size of 32 in cifar10
dimension_l = 100 #-------- taking dimension variable as well
(X_train, y_train), (X_test, y_test) = tensorflow.python.keras.datasets.cifar10.load_data() #----------- loading cifar10 dataset from keras, we dont need testing data in this task
X_train = X_train[y_train.flatten() == 8] #----------------------------------------------flatteirng the image
def Show_image(): #--------------------------------------This function will help in seeing the generated images by the model
row, coloumn = 4,4#------------------------specifying size
e_noise = np.random.normal(0, 1, (row * coloumn, dimension_l)) #-----------Adding noise to the images
Final_image_toshow = generator.predict(e_noise)
Final_image_toshow = 0.5 * Final_image_toshow + 0.5
fig, ckj = plt.subplots(row, coloumn) #----------------used in plotting the image
k = 0
for i in range(row): #------------------for loop will iterate many images at once
for j in range(coloumn):
ckj[i,j].imshow(Final_image_toshow[k, :,:,])
ckj[i,j].axis('off')
k += 1
plt.show()
plt.close()
class Generative_AD_Network():#---------------------------------------creating a class for GAN, it is combined with generator and discriminator
def GAN_Generator(self): #------------------------------------------creating a method for Generator
self.model = Sequential()
self.model.add(Dense(128 * 8 * 8, activation="relu", input_dim=dimension_l)) #--------------------------------creating model for generator
self.model.add(Reshape((8, 8, 128)))
self.model.add(UpSampling2D())
self.model.add(Conv2D(128, kernel_size=3, padding="same"))
self.model.add(BatchNormalization(momentum=0.78))
self.model.add(Activation("relu"))
self.model.add(UpSampling2D())
self.model.add(Conv2D(64, kernel_size=3, padding="same"))
self.model.add(BatchNormalization(momentum=0.78))
self.model.add(Activation("relu"))
self.model.add(Conv2D(3, kernel_size=3, padding="same"))
self.model.add(Activation("tanh"))
self.e_noise = Input(shape=(dimension_l,))
self.image = self.model(self.e_noise)
return Model(self.e_noise, self.image)
def GAN_Discriminator(self): #-------------------------------------------creating a method for Discriminator
self.model = Sequential()
self.model.add(Conv2D(32, kernel_size=3, strides=2, input_shape=image_shape, padding="same"))
self.model.add(LeakyReLU(alpha=0.2)) #----------------------------------------------------------------model for discriminator
self.model.add(Dropout(0.25))
self.model.add(Conv2D(64, kernel_size=3, strides=2, padding="same"))
self.model.add(ZeroPadding2D(padding=((0,1),(0,1))))
self.model.add(BatchNormalization(momentum=0.82))
self.model.add(LeakyReLU(alpha=0.25))
self.model.add(Dropout(0.25))
self.model.add(Conv2D(128, kernel_size=3, strides=2, padding="same"))
self.model.add(BatchNormalization(momentum=0.82))
self.model.add(LeakyReLU(alpha=0.2))
self.model.add(Dropout(0.25))
self.model.add(Conv2D(256, kernel_size=3, strides=1, padding="same"))
self.model.add(BatchNormalization(momentum=0.8))
self.model.add(LeakyReLU(alpha=0.25))
self.model.add(Dropout(0.25))
self.model.add(Flatten())
self.model.add(Dense(1, activation='sigmoid'))
self.image = Input(shape=image_shape)
self.validity = self.model(self.image)
return Model(self.image, self.validity)
a = Generative_AD_Network()#--------------------------------calling object from the class Generative_AD_Network
Discr = a.GAN_Discriminator() #--------------------------------------------assigning dicriminator
Discr.compile(loss='binary_crossentropy', #-----------------------------compiling discimnator
optimizer=Adam(0.0002,0.5),
metrics=['accuracy'])
Discr.trainable = False
generator = a.GAN_Generator() #--------------------------------------------assigning generator
z = Input(shape=(dimension_l,))
image = generator(z)
valid = Discr(image)
Network_Combined = Model(z, valid) #-----------------------------compiling both generator and discriminator
Network_Combined.compile(loss='binary_crossentropy',
optimizer=Adam(0.0002,0.5))
Size_batch=32 #------------------------------setting size of batches
Total_epochs=100000#---------------------------selecting number of total epochs to be run
X_train = (X_train / 127.5) - 1. #--------------------------preprocessing images
alk = np.ones((Size_batch, 1)) #---------------creatng matrix of np ones
alk = alk + 0.05 * np.random.random(alk.shape)
unreal = np.zeros((Size_batch, 1)) #--------------creating matrix of np zeros
unreal = unreal +( 0.05 * np.random.random(unreal.shape) )
for i in tqdm(range(Total_epochs)): #----------------------------------------Final loop
Ind = np.random.randint(0, X_train.shape[0], Size_batch) #-----------------setting index values
images = X_train[Ind]
e_noise = np.random.normal(0, 1, (Size_batch, dimension_l)) #--------------------random noise selection of image
Final_image_toshow = generator.predict(e_noise)
G_loss = Network_Combined.train_on_batch(e_noise, alk)
Loss_D_Real = Discr.train_on_batch(images, alk) #---------------------------------loss of real
Loss_D_Fake = Discr.train_on_batch(Final_image_toshow, unreal) #---------------------LOSs of fake
Loss_D = 0.5 * np.add(Loss_D_Real, Loss_D_Fake)
if i % 2500 == 0: #-----------------------------------------every 2500, image will show up, as the epochs increases the iamge will get better
print("The Epoch number is",+ i)
Show_image()
X_train.shape[0]
e_noise = np.random.normal(size=(40, dimension_l)) #----------------THE final block to see the images
Image_output = generator.predict(e_noise)
Image_output = 0.5 * Image_output + 0.5
f, ax = plt.subplots(5,8, figsize=(16,10))
for i, image in enumerate(Image_output):
ax[i//8, i%8].imshow(image)
ax[i//8, i%8].axis('off')
plt.show()
#------------------------------------ original images
orignal_image=X_train[:40]
orignal_image = 0.5 * orignal_image + 0.5
f, ax = plt.subplots(5,8, figsize=(16,10))
for i, image in enumerate(s):
ax[i//8, i%8].imshow(image)
ax[i//8, i%8].axis('off')
plt.show()